import mountElement from "./mountElement";
import updateTextNode from "./updateTextNode";
import updateNodeElement from "./updateNodeElement";
import createDOM from "./createDOM";
import unmountNode from "./unmountNode";
import diffComponent from "./diffComponent";
export default function diff(virtualDOM, container, oldDOM) {
    const oldVDOM = oldDOM && oldDOM._virtualDOM
    const oldComponent = oldVDOM && oldVDOM.component
    // 判断是否是第一次 第一次的话直接渲染
    if (!oldDOM) {
        mountElement(virtualDOM, container, oldDOM)
        // 节点的type类型不同
    } else if (virtualDOM && virtualDOM.type !== oldVDOM.type && typeof virtualDOM.type !== "function") {
        const newElement = createDOM(virtualDOM)
        oldDOM.parentNode.replaceChild(newElement, oldDOM)
        // 节点的type类型相同
    } else if (typeof virtualDOM.type === "function") {
        diffComponent(virtualDOM, oldComponent, oldDOM, container)
    } else if (virtualDOM && virtualDOM.type === oldVDOM.type) {
        if (virtualDOM.type === "text") {
            updateTextNode(oldDOM, virtualDOM, oldVDOM)
        } else {
            updateNodeElement(oldDOM, virtualDOM, oldVDOM)
        }

        let keyedElement = {}

        let oldChildNodes = oldDOM.childNodes

        for (let i = 0, len = oldChildNodes.length; i < len; i++) {
            let domElement = oldChildNodes[i]
            if (domElement.nodeType === 1) {
                let key = domElement.getAttribute("key")
                if (key) {
                    keyedElement[key] = domElement
                }
            }
        }
        let hasNoKey = Object.keys(keyedElement).length === 0

        if (hasNoKey) {
            virtualDOM.children.forEach((child, i) => {
                diff(child, oldDOM, oldDOM.childNodes[i])
            })
        } else {
            virtualDOM.children.forEach((child, i) => {
                let key = child.props.key
                if (key) {
                    let domElement = keyedElement[key]
                    if (domElement) {
                        if (oldChildNodes[i] && oldChildNodes[i] !== domElement) {
                            oldDOM.insertBefore(domElement, oldChildNodes[i])
                        }
                    } else {
                        mountElement(child, oldDOM, oldChildNodes[i])
                    }
                }
            })
        }


        // 对比 删除多余的子节点
        if (oldChildNodes.length > virtualDOM.children.length) {
            if (hasNoKey) {
                for (let i = oldChildNodes.length - 1; i > virtualDOM.children.length - 1; i--) {
                    unmountNode(oldChildNodes[i])
                }
            } else {
                for (let i = 0; i < oldChildNodes.length; i++) {
                    let oldChild = oldChildNodes[i]
                    let key = oldChild._virtualDOM.props.key
                    let find = false
                    for (let n = 0; n < virtualDOM.children.length; n++) {
                        if (virtualDOM.children[n].props.key === key) {
                            find = true;
                            break
                        }
                    }

                    if (!find) {
                        unmountNode(oldChild)
                    }
                }

            }

        }


    }
}